package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.CustomLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.atguigu.tingshu.vo.order.TrackOrderVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@Operation(summary = "上传音频文件")
	@PostMapping("uploadTrack")
	public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
		Map<String, Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@CustomLogin
	@Operation(description = "保存音频信息")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo vo) {
		trackInfoService.saveTrackInfo(vo);
		return Result.ok();
	}

	@CustomLogin
	@Operation(description = "查询用户音频文件的列表")
	@PostMapping("/findUserTrackPage/{pageNum}/{pageSize}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable("pageNum") Integer pageNum,
														@PathVariable("pageSize") Integer pageSize,
														@RequestBody TrackInfoQuery trackInfoQuery) {
		IPage<TrackListVo> page = trackInfoService.findUserTrackPage(pageNum, pageSize, trackInfoQuery);
		return Result.ok(page);
	}

	@Operation(description = "获取音频文件的详细信息")
	@GetMapping("/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("id") Long id) {
		return Result.ok(trackInfoService.getById(id));
	}

	@CustomLogin
	@Operation(summary = "修改声音")
	@PutMapping("updateTrackInfo/{id}")
	public Result updateById(@PathVariable Long id,
							 @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}

	@CustomLogin
	@Operation(summary = "删除音频信息及音频文件")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	@CustomLogin(required = false)
	@Operation(summary = "根据专辑id分页查询声音")
	@GetMapping("/findAlbumTrackPage/{albumId}/{pageNum}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable("albumId") Long albumId,
															 @PathVariable("pageNum") Integer pageNum,
															 @PathVariable("limit") Integer limit) {
		Page<AlbumTrackListVo> page = new Page<>(pageNum, limit);
		IPage<AlbumTrackListVo> iPage = trackInfoService.findAlbumTrackPage(albumId, page);
		return Result.ok(iPage);
	}

	@Operation(summary = "查询声音统计数据")
	@GetMapping("/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable("trackId") Long trackId) {
		TrackStatVo vo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(vo);
	}

	@CustomLogin(required = false)
	@Operation(summary = "获取声音播放凭证")
	@GetMapping("/getPlayToken/{trackId}")
	public Result<Map<String, Object>> getPlayToken(@PathVariable("trackId") Long trackId) {
		Map<String, Object> map = trackInfoService.getPlayToken(trackId);
		return Result.ok(map);
	}

	@CustomLogin
	@Operation(summary = "查询该专辑中本集声音之后要收费的声音")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result<List<TrackOrderVo>> findUserTrackPaidList(@PathVariable("trackId") Long trackId) {
		List<TrackOrderVo> resultList = trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(resultList);
	}

	/**
	 * 查询用户本次购买的声音列表（远程调用）
	 *
	 * @param trackId
	 * @param count
	 * @return
	 */
	@CustomLogin
	@GetMapping("/findUserNeedPaidTracks/{trackId}/{count}")
	public Result<List<TrackInfo>> findUserNeedPaidTracks(@PathVariable("trackId") Long trackId,
														  @PathVariable("count") Integer count) {
		List<TrackInfo> resultList = trackInfoService.findUserNeedPaidTracks(trackId, count);
		return Result.ok(resultList);
	}
}

